package com.ciwong.epaper.modules.me.presenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import com.ciwong.epaper.common.animdialogactivity.AnimIntegralActivityDialog;
import com.ciwong.epaper.modules.me.bean.IntegralsBean;
import com.ciwong.epaper.modules.me.bean.MedalsBean;
import com.ciwong.epaper.modules.me.bean.UserPointMedalsBean;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.mobilelib.utils.animtoast.ToastAnimDecorate;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class GetPointContral {

    public static void handleUserPointMedals(final Context context,  UserPointMedalsBean userPointMedalsBean) {
        if (userPointMedalsBean == null) return;
        if (userPointMedalsBean.getIntegrals() == null && userPointMedalsBean.getMedals() == null)
            return;
        if (userPointMedalsBean.getIntegrals() != null && userPointMedalsBean.getIntegrals().size() > 0) {//积分存在

            List<IntegralsBean> listIntegral = userPointMedalsBean.getIntegrals();
            for (IntegralsBean integralsBean : listIntegral)
                ToastAnimDecorate.getInstance().makeText(context, spannableCharSequence((int)integralsBean.getIntegral() + "", integralsBean.getTypeName()), ToastAnimDecorate.LENGTH_2000).show();

        }
        if (userPointMedalsBean.getMedals() != null && userPointMedalsBean.getMedals().size() > 0) {//勋章存在

            final List<MedalsBean> listMedal = userPointMedalsBean.getMedals();
            if (listMedal == null || listMedal.size() <= 0) return;
            final LinkedList toastList = ToastAnimDecorate.getInstance().getToastList();
            if (toastList != null && toastList.size() > 0) {//当有toast正在弹时
                ToastAnimDecorate.getInstance().setIEndAllAnimToast(new ToastAnimDecorate.IEndAllAnimToast() {
                    @Override
                    public void endAllAnimToast() {
                        startActivity(context, listMedal);
                    }
                });
            } else {
                startActivity(context, listMedal);
            }
        }else{
            ToastAnimDecorate.getInstance().setIEndAllAnimToast(null);
        }
    }

    private static void startActivity(Context context, List<MedalsBean> listMedal) {
        Intent intent = new Intent(context, AnimIntegralActivityDialog.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(IntentFlag.INTENT_FLAG_OBJ_LIST, (Serializable) listMedal);
        context.startActivity(intent);
    }

    private static CharSequence spannableCharSequence(String integralNum, String typeName) {
        SpannableStringBuilder spanStrNum = new SpannableStringBuilder("+" + integralNum);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#fee350"));
        RelativeSizeSpan sizeSpan22 = new RelativeSizeSpan(2.0f);
        spanStrNum.setSpan(colorSpan, 0, spanStrNum.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spanStrNum.setSpan(sizeSpan22, 0, spanStrNum.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        SpannableString spanStrIntegral = new SpannableString("积分");
        RelativeSizeSpan sizeSpan09 = new RelativeSizeSpan(0.9f);
        spanStrIntegral.setSpan(sizeSpan09, 0, spanStrIntegral.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        spanStrNum.append(spanStrIntegral);
        spanStrNum.append("\n" + typeName);
        return spanStrNum;
    }

}
